<?php
class FollowsController extends AppController {

	var $name = 'Follows';
	var $helpers = array('Html', 'Form', 'timediff');
	var $uses = array('Follow', 'Sale', 'Licitation');
	
	function beforeFilter() {
		parent::beforeFilter();

		if($this->Admin!=1)
		{
			$this->Auth->deny('*');
			$this->Auth->allow('index');
		}
		else
		{
			$this->Auth->allow('*');
		}
	}
	
	function index() {
		$this->pageTitle = 'Following';
		$this->Follow->recursive = 3;
		$conditions = array("Follow.user_id =" =>$this->Auth->user('id'));
		$this->paginate =  array('limit'=> 18);
		$this->set('follows', $this->paginate($conditions));
		$lang = $this->Session->read('Config.language');
		$this->currencyLoad($lang);
	}
	
	function unfollow($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Sale', true));
			$this->redirect(array('action'=>'index'));
		}

	    $result = $this->Follow->find('first',array('conditions'=>array('Follow.user_id'=>$this->Auth->user('id'), 'Follow.sale_id'=>$id)));
		if ($this->Follow->delete($result['Follow']['id']) )
		{
			$this->Session->setFlash(__('Unfollowed ', true));
			$this->redirect(array('controller' => 'sales','action'=>'view',$id));
		}
		$this->Session->setFlash(__('FAIL! ', true));
		$this->redirect(array('controller' => 'sales','action'=>'view',$id));
		
		
	}

	function licitate($id = null, $page, $params) {



		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Sale', true));
			$this->redirect(array('action'=>'index'));
		}

		if (!$this->Auth->user('id')) {
			$this->Session->setFlash(__('Please login', true));
			$this->redirect(array('controller' => 'users','action'=>'login'));
		}

		if ($this->Auth->user('nrLicitations')==0)
		{
			$this->Session->setFlash(__('Insufficient licitations', true));
			$this->redirect(array('action'=>'index'));
		}

		$this->data = $this->Sale->read(null, $id);

		$price = $this->data['Sale']['currentPrice'] + 0.01;
		$timeEnd = date('Y-m-d  H:i:s', +strtotime("+15 seconds",strtotime($this->data['Sale']['timeEnd'])));
		$licitationTime = new DateTime();

		if(strtotime($this->data['Sale']['timeEnd']) < strtotime(date('Y-m-d  H:i:s',$licitationTime->format('U'))))
		{
			$this->Session->setFlash(__('Auction already over', true));
			$this->redirect(array('action'=>'index'));
		}

		App::import('Model', 'User');
		$user = new User();
		$user->id = $this->Auth->user('id');


		if ($this->Sale->save(array('user_id' => $this->Auth->user('id'), 'currentPrice' => $price,
			 'timeEnd' => $timeEnd, false)) 
		&&
		$this->Licitation->save(array('user_id' => $this->Auth->user('id'),
			 'timeLicitation'=>date('Y-m-d  H:i:s',$licitationTime->format('U')),
			 'price'=>$price,
			 'sale_id'=>$this->data['Sale']['id']))
		&&
		$user->saveField('nrLicitations', $this->Auth->user('nrLicitations') - 1))
		{
			$this->Session->write('Auth.User.nrLicitations', $this->Auth->user('nrLicitations') - 1);
			$this->redirect(array('action'=>$page,$params));

		}
			

	}
	
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Follow.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('follow', $this->Follow->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Follow->create();
			if ($this->Follow->save($this->data)) {
				$this->Session->setFlash(__('The Follow has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Follow could not be saved. Please, try again.', true));
			}
		}
		$sales = $this->Follow->Sale->find('list');
		$users = $this->Follow->User->find('list');
		$this->set(compact('sales', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Follow', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Follow->save($this->data)) {
				$this->Session->setFlash(__('The Follow has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Follow could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Follow->read(null, $id);
		}
		$sales = $this->Follow->Sale->find('list');
		$users = $this->Follow->User->find('list');
		$this->set(compact('sales','users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Follow', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Follow->del($id)) {
			$this->Session->setFlash(__('Follow deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}



}
?>